package com.tt.utils.wordUtils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.Tables;
import com.deepoove.poi.policy.TextRenderPolicy;
import com.tt.utils.wordUtils.entity.*;
import com.tt.utils.wordUtils.renderplicy.*;
import lombok.SneakyThrows;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title: WordBaseUtils
 * @Author zl
 * @Package com.oberyun.goHAZOP.utils.wordUtils
 * @Date 2025/9/8 11:35
 * @description: 导出word工具类
 */

public class ExportWordUtils {

    public static void export(InputStream templatePath, String outputPath, WordBaseData wordBaseData) throws Exception {
        Map<String,Object> maps = new HashMap<>();
        ConfigureBuilder config = Configure.builder();
        //判断是否有文本
        if(wordBaseData.getTextContents() != null){
            for (TextContent textContent : wordBaseData.getTextContents()) {
                config.bind(textContent.getKey(), new TextRenderPolicy());
                maps.put(textContent.getKey(),textContent.getContent());
            }
        }
        //判断是否有图片
        if(wordBaseData.getImageContents() != null){
            for (ImageContent imageContent : wordBaseData.getImageContents()) {
                config.bind(imageContent.getKey(),new ImageRenderPolicy());
                maps.put(imageContent.getKey(),imageContent);
            }
        }
        //判断是否有表格
        if(wordBaseData.getTableContents() != null){
            for (TableContent tableContent : wordBaseData.getTableContents()) {
                config.bind(tableContent.getKey(), new TableRenderV2Policy());
//                config.bind(tableContent.getKey(), new TableRenderPolicy());
                maps.put(tableContent.getKey(),tableContent.getTableBody());
            }
        }
        //判断是否有超链接
        if(wordBaseData.getLinkContents() != null){
            for (LinkContent linkContent : wordBaseData.getLinkContents()) {
                config.bind(linkContent.getKey(),new HyperlinkRenderPolicy());
                maps.put(linkContent.getKey(),linkContent);
            }
        }
        Configure build = config.build();
        XWPFTemplate template = XWPFTemplate.compile(templatePath, build).render(maps);
        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            template.write(out);
        }
        template.close();
    }

    public static WordBaseData sampleData() {
        WordBaseData wordBaseData = new WordBaseData();
        wordBaseData.setTextContents(Arrays.asList(new TextContent("name","张三")));
        TableContent.TableBody tableBody = new TableContent.TableBody();
        tableBody.setTitles(Arrays.asList(
            new TableContent.TableTitleData("序号"),
            new TableContent.TableTitleData("姓名"),
            new TableContent.TableTitleData("性别")
        ));
        tableBody.setBody(Arrays.asList(
            Arrays.asList(
                new TableContent.TableCellData("1",0),
                new TableContent.TableCellData("张三",0),
                new TableContent.TableCellData("男",0)
            ),
            Arrays.asList(
                new TableContent.TableCellData("2",0),
                new TableContent.TableCellData("张三",0),
                new TableContent.TableCellData("男",0)
            )
        ));
        tableBody.setColumnCount(3);
        wordBaseData.setTableContents(Arrays.asList(new TableContent("table",tableBody),new TableContent("table1",tableBody)));

        return wordBaseData;
    }

    @SneakyThrows
    public static void main(String[] args) {
        String templatePath = "E:\\1\\123.docx";
        String outputPath = "E:\\1\\output.docx";
        export(new FileInputStream(templatePath), outputPath, sampleData());
        System.out.println("导出成功：" + outputPath);
    }



}
